package com.guigu.www.service.imp;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.protobuf.FieldMask;
import com.guigu.www.mapper.*;
import com.guigu.www.pojo.*;
import com.guigu.www.service.MProceduringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.List;

@Service
public class MProceduringServiceImpl  extends ServiceImpl<MProceduringMapper, MProceduring>
implements MProceduringService {
    //生产总表数据
    @Autowired
    MManufactureMapper mManufactureMapper;
    //生产工序表
    @Autowired
    MProcedureMapper mProcedureMapper;
    //创建生产工序过程记录mapper
    @Autowired
    MProceduringMapper mProceduringMapper;
    //创建工序物料表
    @Autowired
    MProcedureModuleMapper mProcedureModuleMapper;

    //生产工序物料过程记录
    @Autowired
    MProcedureModulingMapper mProcedureModulingMapper;
    //入库表mapper
    @Autowired
    GatherMapper gatherMapper;
    //产品mapper
    @Autowired
    FileMapper fileMapper;
    //入库明细数据
    @Autowired
    GatherDetailsMapper gatherDetailsMapper;
    //创建流水号
    @Autowired
    IdUtils idUtils;
    @Override
    //产品登记  添加生产工序数据  和生产工序过程记录
    public boolean addMproceduring(MProceduring mProceduring) {
        mProceduring.setRegCount(1);
        //根据当前物料查询单价
        mProceduring.setSubtotal(mProceduring.getCostPrice()*mProceduring.getLabourHourAmount());
        int insert = mProceduringMapper.insert(mProceduring);
        boolean f=false;
        int i=0;
        int parentid=0;
        System.out.println(mProceduring);
        for (MProcedureModule mproceduremodule : mProceduring.getMproceduremodules()) {
            //生产工序物料过程记录
            MProcedureModuling mProcedureModuling=new MProcedureModuling();
            parentid=mproceduremodule.getParentid();
            mProcedureModuling.setParentId(mProceduring.getId());
            mProcedureModuling.setDetailsNumber(mProceduring.getDetailsNumber());
            mProcedureModuling.setProductId(mProceduring.getProcedureId());
            mProcedureModuling.setProductName(mproceduremodule.getProductName());
            mProcedureModuling.setCostPrice(mproceduremodule.getCostPrice());
            mProcedureModuling.setAmount(mproceduremodule.getIsnum());
            mProcedureModuling.setSubtotal(mProcedureModuling.getCostPrice()*mProcedureModuling.getAmount());
            //生产工序物料表
            mproceduremodule.setRealSubtotal(mproceduremodule.getRealAmount()*mproceduremodule.getCostPrice());
            i+=mProcedureModulingMapper.insert(mProcedureModuling);
            //修改工序物料表
            mProcedureModuleMapper.updateById(mproceduremodule);
        }
        //修改生产工序表状态
        mProcedureMapper.updateMoricedure(parentid);
        //修改总表状态
        mManufactureMapper.updateMmanufacturebyid(mProceduring.getParentId());
        if(insert>0 && i==mProceduring.getMproceduremodules().size()){
            f=true;
        }
        return f;
    }

    //产品登记审核  修改状态 数据
    @Override
    public boolean updateMproceduring(MProceduring mProceduring) {
        //根据当前父id编号查询
        List<MProceduring> mProceduring1=mProceduringMapper.queryMproceduringqueryByparenid(mProceduring.getParentId());
        boolean f=false;
        //修改生产工序物料表
        int i=0;
        int parentid=0;
        String PROCEDUREFINISHTAG="G004-2";
        int numsi=0;
        for (MProcedureModule mproceduremodule : mProceduring.getMproceduremodules()) {
            System.out.println("1"+mproceduremodule);
            mproceduremodule.setRealAmount(mproceduremodule.getRealAmount()+mproceduremodule.getIsnum());
            i+=mProcedureModuleMapper.updateMprocedureModule(mproceduremodule);
            System.out.println(mproceduremodule.getAmount());
            System.out.println(mproceduremodule.getRealAmount());
            if(mproceduremodule.getAmount().intValue()!=mproceduremodule.getRealAmount().intValue()){
                numsi++;
            }
            //修改确定数量为0
            mProcedureModuleMapper.updateMprocedureModules(mproceduremodule);
        }
        System.out.println(numsi);
        if(numsi==0){
            PROCEDUREFINISHTAG="G004-3";
        }

        //组装数据 //根据id查询
        //循环当前 所有的工序过程 和工序物料过程
        //本次工时
        Integer labourhouramount=0;
        //工时成本
        Integer subtotal=0;
        //物料成本小计
        Integer subtotalnum=0;
        int pid=0;
        for (MProceduring proceduring : mProceduring1) {
            parentid=proceduring.getParentId();
            pid=proceduring.getParentId();
            labourhouramount+=proceduring.getLabourHourAmount();
            subtotal+=proceduring.getSubtotal();
            for (MProcedureModuling mprocedureModuling : proceduring.getMprocedureModulings()) {
                System.out.println("3"+mprocedureModuling);
                //获取总物料价格
                subtotalnum+=mprocedureModuling.getSubtotal();
            }
        }
        //修改生产记录过程表
        mProceduring.setRegCount(mProceduring1.size());
        mProceduringMapper.updatemprocefuringbyid(pid,mProceduring);
        //查询
        MProcedure mProcedure=mProcedureMapper.queryBypreantid(mProceduring.getParentId());
        //实际工时
        mProcedure.setRealLabourHourAmount(labourhouramount);
        //实际工时成本
        mProcedure.setRealSubtotal(subtotal);
        //物料成本小计
        mProcedure.setRealModuleSubtotal(subtotalnum);
        mProcedure.setProcedureFinishTag(PROCEDUREFINISHTAG);
        //修改生产工序表
        mProcedureMapper.updatemprocedurebyid(mProcedure,mProceduring.getParentId());
        //修改总表数据
        //查询总表数据
        MManufacture mManufacture= mManufactureMapper.queryBymmanfactherbyid(parentid);
        System.out.println(mManufacture);
        mManufacture.setRealModuleCostPriceSum(mManufacture.getRealModuleCostPriceSum()+subtotalnum);
        mManufacture.setRealLabourCostPriceSum(mManufacture.getRealLabourCostPriceSum()+subtotal);
        mManufacture.setManufactureProcedureTag("S002-0");
        mManufactureMapper.updatemmanufacturestatus(mManufacture,parentid);
        if(mProceduring.getMproceduremodules().size()==i){
            f=true;
        }

        return f;
    }

    @Override
    public boolean updateMprkcedhringshow(String manufactureId, int amout, int detailsNumber, int id) {
        boolean f1=false;
        boolean f=false;
        if(detailsNumber!=3){
            //修改生产表数据
            f1=mProcedureMapper.updateMprkcedhringshow(amout,id)>0?true:false;
            //根据id查询工序表数据
            MProcedure mProcedure = mProcedureMapper.selectById(id);
            //修改状态
            //修改总表数据
            mManufactureMapper.updatemprocedurestatus(mProcedure.getParentId());

        }else {
            //修改状态   修改生产表数据
            f1=mProcedureMapper.updateMprkcedhringshow(amout,id)>0?true:false;
            //根据id查询工序表数据
            MProcedure mProcedure = mProcedureMapper.selectById(id);
            //修改总表数据
            mManufactureMapper.updatemprocedurestatuson(mProcedure.getParentId(),mProcedure.getRealAmount());
            //根据派工单编号查询当前数据
           MManufacture mManufacture=mManufactureMapper.querybymanufactureId(manufactureId);
           //根据产品编号查询
           File file=fileMapper.queryByfileid(mManufacture.getProductId());
           //主装入库调度单数据
            //创建对象
            Gather gathers=new Gather();
            gathers.setRegisterTime(mManufacture.getRegisterTime());
            Gather gather=new Gather();
            gather.setGatherId(idUtils.getid6(gathers));
            gather.setReason("R001-1");
            gather.setStorer(mManufacture.getRegister());
            gather.setReasonexact("50013424");
            gather.setAmountSum(mManufacture.getTestedAmount().toString());
            gather.setCostPriceSum(mManufacture.getRealModuleCostPriceSum().toString());
            gather.setGatheredAmountSum("0");
            gather.setRemark(mManufacture.getRemark());
            gather.setRegister(mManufacture.getRegister());
            gather.setRegisterTime(mManufacture.getRegisterTime());
            gather.setChecker(mManufacture.getChecker());
            gather.setCheckTime(mManufacture.getCheckTime());
            gather.setCheckTag("S001-1");
            gather.setGatherTag("");
            gatherMapper.insert(gather);
            //添加明细数据
            GatherDetails gatherDetails=new GatherDetails();
            gatherDetails.setParentId(gather.getId());
            gatherDetails.setProductId(mManufacture.getProductId());
            gatherDetails.setProductName(mManufacture.getProductName());
            gatherDetails.setProductDescribe(mManufacture.getProductDescribe());
            gatherDetails.setAmount(mManufacture.getTestedAmount().toString());
            gatherDetails.setAmountUnit("件");
            gatherDetails.setCostPrice(file.getCostprice());
            Double nums=Double.parseDouble(gatherDetails.getAmount())*Double.parseDouble(gatherDetails.getCostPrice());
            gatherDetails.setSubtotal(nums.toString());
            gatherDetails.setGatherTag("");
            gatherDetailsMapper.insert(gatherDetails);

        }
        if(f1){
            f=true;
        }
        return f;
    }

}
